package com.woniuxy.common;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.woniuxy.dto.CarAccidentDto;
import com.woniuxy.service.CarAccidentService;

import java.util.ArrayList;
import java.util.List;

public class CarAccidentExcelListener extends AnalysisEventListener<CarAccidentDto> {
    private CarAccidentService carAccidentService;

    public CarAccidentExcelListener(CarAccidentService carAccidentService) {
        this.carAccidentService = carAccidentService;
    }

    //创建一个集合保存所有导入的数据
    private List<CarAccidentDto> carAccidentList= new ArrayList<>();
    @Override
    public void invoke(CarAccidentDto carAccidentDto, AnalysisContext analysisContext) {
        //每条数据添加到集合中
        carAccidentList.add(carAccidentDto);
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        //写入到数据库中
        rowCount = carAccidentService.insertBatch(carAccidentList);
    }
    //影响行数
    private int rowCount;
    //获取导入的行数
    public int getRowCount() {
        return this.rowCount;
    }
}
